/*
 * Copyright (c) 2001, Aslak Hellesøy, BEKK Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of BEKK Consulting nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package middlegen.plugins.ibatis;

import java.io.File;

import middlegen.FileProducer;
import middlegen.MiddlegenException;
import middlegen.javax.JavaPlugin;

/**
 * This is a plugin to generate iBatis code from the database.
 *
 * @author David Channon
 * @author <a href="mailto:p.mouawad@ubik-ingenierie.com">Philippe Mouawad</a>
 * @created 15. may 2005
 * @middlegen.plugin
 *       name = "ibatis"
 *
 */
public class IBatisPlugin extends JavaPlugin {

   /** The name of the database/application */
   private String databaseName;
   /** The JDBC URL used to connect to the database */
   private String databaseURL;
   /** The JDBC Driver used to connect to the database */
   private String databaseDriver;
   /** The username used to connect to the database */
   private String databaseUser;
   /** The password used to connect to the database */
   private String databasePassword;
   /** The name of database schema connected to */
   private String databaseSchema;
   /** The name of database catalog used */
   private String databaseCatalog;
   /** The JNDI name of the datasource used to connect to the database */
   private String datasourceJNDIName;
   /** The directory in which to store the generated Torque.properties file. */
   private File propertiesDir;

   /** Constructor for IBatisPlugin */
   public IBatisPlugin() {
      super();
      setDisplayName("IBatis");
   }

   /**
    * Sets the database/application name
    *
    * @param dbName The new database/application name
    */
   public void setDatabaseName(String dbName) {
      databaseName = dbName;
   }


   /**
    * Sets the database catalog name
    *
    * @param catalog The new database catalog name
    */
   public void setDatabaseCatalog(String catalog) {
      databaseCatalog = catalog;
   }


   /**
    * Sets the JDBC driver
    *
    * @param driver The new JDBC driver
    */
   public void setDatabaseDriver(String driver) {
      databaseDriver = driver;
   }


   /**
    * setDatabasePassword
    *
    * @param string
    */
   public void setDatabasePassword(String string) {
      databasePassword = string;
   }


   /**
    * Sets the name of the database schema
    *
    * @param schema The new database schema name
    */
   public void setDatabaseSchema(String schema) {
      databaseSchema = schema;
   }


   /**
    * Sets the JDBC URL for the database
    *
    * @param url The new JDBC url
    */
   public void setDatabaseURL(String url) {
      databaseURL = url;
   }


   /**
    * Sets the username for the database connection
    *
    * @param username The database username
    */
   public void setDatabaseUser(String username) {
      databaseUser = username;
   }


   /**
    * Sets the datasource JNDI name
    *
    * @param jndiName The new datasource JNDI name
    */
   public void setDatasourceJNDIName(String jndiName) {
      datasourceJNDIName = jndiName;
   }


   /**
    * Sets the directory in which to store the generated Torque.properties
    *
    * @param dir The new directory
    */
   public void setPropertiesDir(File dir) {
      propertiesDir = dir;
   }


   /**
    * Gets the ColumnDecorator for this plugin
    *
    * @return The ColumnDecoratorClass for this plugin
    * @see middlegen.Plugin#getColumnDecoratorClass()
    */
   public Class getColumnDecoratorClass() {
      return IBatisColumn.class;
   }


   /**
    * Gets the TableDecorator for this plugin
    *
    * @return The TableDecoratorClass for this plugin
    * @see middlegen.Plugin#getTableDecoratorClass()
    */
   public Class getTableDecoratorClass() {
      return IBatisTable.class;
   }


   /**
    * Gets the database/application name
    *
    * @return The database/application name
    */
   public String getDatabaseName() {
      return databaseName;
   }


   /**
    * Gets the name of the database catalog
    *
    * @return The name of the database catalog
    */
   public String getDatabaseCatalog() {
      return databaseCatalog;
   }


   /**
    * Gets the JDBC driver
    *
    * @return The JDBC driver
    */
   public String getDatabaseDriver() {
      return databaseDriver;
   }


   /**
    * Gets the database password
    *
    * @return The database password
    */
   public String getDatabasePassword() {
      return databasePassword;
   }


   /**
    * Gets the name of the database schema
    *
    * @return The name of the database schema
    */
   public String getDatabaseSchema() {
      return databaseSchema;
   }


   /**
    * Gets the JDBC URL for the connection to the database
    *
    * @return The JDBC URL
    */
   public String getDatabaseURL() {
      return databaseURL;
   }


   /**
    * Gets the database username
    *
    * @return The database username
    */
   public String getDatabaseUser() {
      return databaseUser;
   }


   /**
    * Gets the datasource JNDI name
    *
    * @return The datasource JNDI name
    */
   public String getDatasourceJNDIName() {
      return datasourceJNDIName;
   }

   /**
    * Validate that all required data and resources are available
    *
    * @exception MiddlegenException If some data or resource is missing, or if the plugin cannot successfully execute
    * @see middlegen.Plugin#validate()
    */
   public void validate() throws MiddlegenException {
      super.validate();
      //      if (databaseName == null || databaseName.length() == 0) {
      //         throw new MiddlegenException("Database/application name not specified");
      //      }
      //      if (databaseDriver == null || databaseDriver.length() == 0) {
      //         throw new MiddlegenException("Database driver not specified");
      //      }
      //      if (datasourceJNDIName == null) {
      //         if (databaseURL == null || databaseURL.length() == 0) {
      //            throw new MiddlegenException("Database URL not specified");
      //         }
      //         if (databaseUser == null || databaseUser.length() == 0) {
      //            throw new MiddlegenException("Database user not specified");
      //         }
      //      }
      //      if (propertiesDir == null) {
      //         throw new MiddlegenException("Properties directory (destination for Torque.properties) not specified");
      //      }
   }


   /**
    * Registers the file producers for this plugin
    *
    * @see middlegen.Plugin#registerFileProducers()
    */
   protected void registerFileProducers() {
      addConfiguredFileproducer(new FileProducer(getDestinationDir(),
            "{0}.xml", getClass().getResource("iBatis-xml.vm")));
      addConfiguredFileproducer(new FileProducer(getDestinationDir(),
            "{0}.java", getClass().getResource("iBatis-bean.vm")));
   }
}
